package com.x.game.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 值对象：棋盘快照
 * 不可变，行列数由外部 BoardSize 保证
 */
public final class BoardSnapshot {

    private final List<List<Integer>> slots; // 外层 List 是行，内层 List 是列

    /* ---------- 构造 ---------- */
    public BoardSnapshot(List<List<Integer>> slots, BoardSize size) {
        Objects.requireNonNull(slots, "slots 不能为 null");
        if (slots.size() != size.rows()) {
            throw new IllegalArgumentException("行数与 BoardSize 不符");
        }
        for (List<Integer> row : slots) {
            if (row == null || row.size() != size.cols()) {
                throw new IllegalArgumentException("列数与 BoardSize 不符");
            }
        }
        // 防御性深拷贝
        this.slots = new ArrayList<>(size.rows());
        for (List<Integer> row : slots) {
            this.slots.add(List.copyOf(row));
        }
    }

    /* ---------- 静态工厂 ---------- */
    public static BoardSnapshot of(List<List<Integer>> slots, BoardSize size) {
        return new BoardSnapshot(slots, size);
    }

    /* ---------- 只读访问 ---------- */
    public List<List<Integer>> getSlots() {
        return slots; // 已不可变，直接返回
    }

    public int getValue(int row, int col) {
        return slots.get(row).get(col);
    }

    /* ---------- 值对象相等性 ---------- */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BoardSnapshot)) return false;
        BoardSnapshot that = (BoardSnapshot) o;
        return slots.equals(that.slots);
    }

    @Override
    public int hashCode() {
        return Objects.hash(slots);
    }

    @Override
    public String toString() {
        return "BoardSnapshot" + slots;
    }
}
